package com.ddh.grafanaes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ddh.grafanaes.model.User;
import com.google.common.collect.Sets;
import com.google.common.net.InetAddresses;
import org.apache.commons.lang3.RandomStringUtils;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author dongdh
 * @BatchToEs
 * @2019/8/22 22:15
 */


public class BatchToEs {

    String[] level = new String[]{"A", "B", "C", "D"};
    int[] sxe = new int[]{0, 1};

    //纬度，精度
    String[] location = new String[] {
            "39.90,116.40","38.05,114.52","41.80,123.43", "31.23,121.47",
            "32.07,118.78"

    };

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");


    TransportClient transportClient = null;

    @Before
    public void before() {
        String clusterName = "my-es";
        String ip = "ip";
        int port = 9300;
        Settings settings = Settings.builder()
                .put("client.transport.ping_timeout", "30s")
                .put("cluster.name", clusterName)
               // .put("client.transport.sniff", true)
                .build();
        transportClient = new PreBuiltTransportClient(settings);

        transportClient.addTransportAddress(
                new TransportAddress(InetAddresses.forString(ip), 9300));
    }

    private User buildUser(Date date) {
        User user = new User();
        user.setAge(Integer.valueOf(RandomStringUtils.randomNumeric(2)));
        user.setId(0L); //这个无意义
        user.setLevel(level[Integer.valueOf(RandomStringUtils.randomNumeric(3)) % 4]);
        user.setSxe(sxe[Integer.valueOf(RandomStringUtils.randomNumeric(3)) % 2]);
        user.setLocation(location[Integer.valueOf(RandomStringUtils.randomNumeric(3)) % location.length]);
        user.setCreateTime(date);

        return user;
    }

    @Test
    public void test() throws ParseException {

        System.out.println(transportClient);

        //创建三天的数据
        day("2019.08.23");
        day("2019.08.22");
        day("2019.08.21");
        day("2019.08.20");

    }

    private void day(String day) throws ParseException {
        Date parse = sdf.parse(day + " 00:00:01");

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(parse);
        int dy = calendar.get(Calendar.DAY_OF_YEAR);
        //List<User> userList = new ArrayList<>();
        while (true) {


            User user = buildUser(calendar.getTime());
           // userList.add();

            calendar.add(Calendar.MINUTE, 1);
            int dayofyear = calendar.get(Calendar.DAY_OF_YEAR);
            long start = System.currentTimeMillis();
            String source = JSON.toJSONString(user);
            Map map = JSONObject.parseObject(source, Map.class);
            IndexResponse indexResponse = transportClient.prepareIndex("user-" + day, "type").setSource(map).get();


            System.out.println(calendar.getTime() + ",,time=" + (System.currentTimeMillis() - start));
            if (dayofyear != dy) {
                break;
            }

        }


        //System.out.println(userList.size());
/*        BulkRequestBuilder bulk = transportClient.prepareBulk("user-" + day, "type");

        userList.forEach(user -> {
            String source = JSON.toJSONString(user);
            Map map = JSONObject.parseObject(source, Map.class);
            IndexRequestBuilder indexRequestBuilder = transportClient.prepareIndex().setSource(map);
            bulk.add(indexRequestBuilder);
        });

        BulkResponse bulkResponse = bulk.get();
        //服务器太差 批量太慢了
        System.out.println(bulkResponse);*/

       // return userList;

    }

}
